/**
 * Created by lixiaoping on 15/10/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready,Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            this.model = new Model({
                container: 'main-view',
                template: 'order-tpl',
                tag_name: 'div',
                url: _global.url.api + 'order_refund/'+id,
                callback: function() {
                    loaded();
                }
            });
            this.model.fetch();
        },

        event: function() {
            var _this = this;
            console.log(this.model.data);

            show_refund_list(id);
        }
    };
    var show_refund_list =function(order_id){
        this.collection = new Collection({
            container: 'data-list_goods',
            template: 'renfun_date_tpl',
            url: _global.url.api + 'refund_list',
            condition: {order_id:order_id},
            model: {
                url: _global.url.api + 'order_refund',
                event: {
                    '.item-agree:click': 'tuihuo_agree_status',
                    '.item-reject:click':'tuihuo_reject_status'
                },
                tuihuo_agree_status:function(e){
                    e.preventDefault();
                    var _this = this;
                    dialog({
                        title : "当前操作订单：" + this.data.order_sn,
                        content: $('#tuihuo-tpl').html(),
                        modal:true,
                        width: 500,
                        ok: function() {
                            _this.data.refund_status = 2;
                            _this.data.tuihuo_note = $("#tuihuo_note").val();
                            _this.data.tui_type = $('input[name="tui_type"]:checked').val();
                            if(_this.data.tui_type==2){
                                _this.data.tui_amount = $("#tui_amount").val();
                                if( _this.data.tui_amount==''|| _this.data.tui_amount<=0){
                                    Alert({msg:'请输入金额',type:'error'});
                                    return false;
                                }
                                if( Number(_this.data.tui_amount)>Number(_this.data.paid_price)){
                                    Alert({msg:'金额不能大于实付金额',type:'error'});
                                    return false;
                                }

                            }
                            _this.put();
                            show_refund_list(order_id);
                        },cancel: function() {}
                    }).show();
                    $('.tuihuo-type').show();
                    $(".tuihuo-content").show();
                    $(".tuihuo-desc").show();
                    $("input[name='tui_type']").on("click",function(e){
                        if($(this).val()==2){
                            $(".tuihuo-amount").show();
                        }else{
                            $(".tuihuo-amount").hide();
                        }
                    });
                    $("#tui_amount").on('blur',function(e){
                        e.preventDefault();
                        var reg =/^[0-9]+(\.[0-9]{1,2})?$/;
                        if($(this).val()!==''){
                            if(!reg.test($(this).val())){
                                Alert({msg:'金额格式不正确',type:'error'});
                                $(this).val('');
                            }
                        }
                    });
                },
                tuihuo_reject_status:function(e){
                    e.preventDefault();
                    var _this = this;
                    dialog({
                        title : "当前操作订单：" + this.data.order_sn,
                        content: $('#tuihuo-tpl').html(),
                        modal:true,
                        width: 500,
                        ok: function() {
                            _this.data.refund_status = 3;
                            _this.data.tuihuo_note = $('[name="tuihuo_note"]').val();
                            _this.put();
                            show_refund_list(order_id);
                        },cancel: function() {}
                    }).show();
                    $(".tuihuo-content").show();
                }
            },
            callback: function() {
                loaded();
            }
        });
        this.collection.reload();
    }

    new index();

});